$(function() {
	if($(".out").attr("data-type")==0){
		$(".ymb .self").css("background-color","#880000");
	}else if($(".out").attr("data-type")==1){
		$(".ymb .day").css("background-color","#880000");
	}else{
		$(".ymb .airport").css("background-color","#880000");
	}
	$(".sort .content div").click(function() {
		// 获取点击对象
		var target = $(this);
		select(target);// 获取选择的属性
		sendData(target,1);// ajax传数据
		$(".M-box").pagination({
			pageCount:getCount(),
			/*totalData: $(".cars").attr("data-count"),
			showData:5,*/
	        coping:true,
	        homePage:'首页',
	        endPage:'末页',
	        prevContent:'上页',
	        nextContent:'下页',
	        callback:function(api){
	        	/*console.log("当前页："+api.getCurrent());
	    		console.log("总页："+api.getPageCount());
	        	console.log(getCount());*/
	        	api.setPageCount(getCount());//设置总页数
	    		sendData($(".tag"),api.getCurrent());
	        }
		});
	});
	
	
	// 绑定选中分类标签的X
	$(".tag").on(
			"click",
			".img",
			function() {
				var target = $(this).parent();
				if (target.parent().is(".styleSelected")) {
					$(".style .all").addClass("select").siblings(".select")
							.removeClass("select");
				} else if (target.parent().is(".priceSelected")) {
					$(".price .all").addClass("select").siblings(".select")
							.removeClass("select");
				} else {
					if (target.parent().children().length == 1) {
						$(".brand .all").addClass("select").siblings(".select")
								.removeClass("select");
					} else {
						var str = target.attr("class");
						$(".brand .select[id='" + str + "']").toggleClass(
								"select");
					}
				}
				sendData(target,1);
				target.remove();
				isNull();
				$(".M-box").pagination({
					pageCount:getCount(),
					/*totalData: $(".cars").attr("data-count"),
					showData:5,*/
			        coping:true,
			        homePage:'首页',
			        endPage:'末页',
			        prevContent:'上页',
			        nextContent:'下页',
			        callback:function(api){
			        	/*console.log("当前页："+api.getCurrent());
			    		console.log("总页："+api.getPageCount());
			        	console.log(getCount());*/
			        	api.setPageCount(getCount());//设置总页数
			    		sendData($(".tag"),api.getCurrent());
			        }
				});
			});

	// 绑定每个品牌的鼠标悬浮事件，显示该品牌logo
	$(".sort .brand").on("mouseover", "div:not('.all')", function() {
		var target=$(this);
		var id = target.attr("id");
		target.siblings("[data-id='" + id + "']").css({
			"display" : "block",
			"left" : target.offset().left-30,
			"top" : target.offset().top-70
		});
	});
	// 鼠标移开时logo消失
	$(".sort .brand").on("mouseout", "div:not('.all')", function() {
		$(this).siblings(".logo").css("display", "none");
	});
	// 排序
	$(".seq span").click(function() {
		var target = $(this);
		target.addClass("ok").siblings().removeClass("ok");
		sendData(target,1);
	});

	// 绑定每个按钮的点击事件
	$(".cars").on("click", ".btn", function() {
		// console.log($(this).attr("data-id"));
		var quDay=$("input[name=takeTime]").val();
		var quTime=$("select[name=takeTime] option:selected").text();
		var quLocation=$("select[name=qulocation] option:selected").attr("data-id");
		var quStore=$("select[name=qustore] option:selected").attr("data-id");
		var huanDay=$("input[name=returnTime]").val();
		var huanTime=$("select[name=returnTime] option:selected").text();
		var huanLocation=$("select[name=huanlocation] option:selected").attr("data-id");
		var huanStore=$("select[name=huanstore] option:selected").attr("data-id");
		var type=$(".out").attr("data-type");
		window.location.href = "goods/order?id=" + $(this).attr("data-id")+"&type="+type+"&quday="+quDay+"&qutime="+quTime+"&qulocation="+quLocation+"&qustore="+quStore+"&huanday="+huanDay+"&huantime="+huanTime+"&huanlocation="+huanLocation+"&huanstore="+huanStore;
	});
	/**
	 * 使用ajax传入数据
	 * 
	 * @param target
	 * @returns
	 */
	function sendData(target,pageCurrent) {
		var style = target.parents(".out").find(".style").find(".select")
				.text();
		var brandAll = target.parents(".out").find(".brand").find(".select");
		var maxIndex = brandAll.length - 1;
		var brand = "";
		// 拼接品牌的字符串
		brandAll.text(function(index, content) {
			brand += (index == maxIndex) ? "'" + content + "'" : "'" + content
					+ "',";
		});
		var price = target.parents(".out").find(".price").find(".select")
				.text();
		// console.log(style+"和"+brand+"和"+price);
		var seq = target.parents(".out").find(".ok").text();
		$.get("show", {
			style : style,
			brand : brand,
			price : price,
			seq : seq,
			pageNo:pageCurrent
		}, function(datas) {
			var index=layer.msg('加载中', {
				  icon: 16
			});
			// 每点击一次就清空列表
			$(".cars").html("");
			// 判断传过来的json对象是否为空
			if ($.isEmptyObject(datas)) {
				// alert("找不到哈哈哈");
				// 为空则输出提示信息
				layer.close(index);
				var error = $("<div>", {
					"class" : "no"
				}).text("对不起，该分类下没有商品！");
				$(".cars").append(error);
			} else {
				showCars(datas);// 遍历商品
				layer.close(index);
			}
		});
	}
	
	/**
	 * 遍历商品信息
	 * 
	 * @param datas
	 * @returns
	 */
	function showCars(datas) {
		$.each(datas, function(i, item) {
			// 汽车图片
			var img = $("<img>", {
				"alt" : "汽车图片",
				"src" : "statics/image/upload/cars/"+item.cphoto
			});
			var image = $("<div>", {
				"class" : "image"
			}).append(img);
			var tb;
			if(item.cNew==1){
				tb = $("<img>", {
					"alt" : "新品",
					"src" : "statics/image/goods/new.png"
				});
			}
			if(item.cHot==1){
				tb = $("<img>", {
					"alt" : "热销",
					"src" : "statics/image/goods/hot.png"
				});
			}
			// 汽车名称
			var cname = $("<p>").text(item.cname).append(tb);
			// 汽车描述
			var cinfo = $("<p>").text(item.cinfo);
			// 汽车类型
			var csName = $("<p>").text(item.csName);
			var info = $("<div>", {
				"class" : "info"
			}).append(cname).append(cinfo).append(csName);
			// 汽车价格
			var span = $("<span>").text("￥" + item.cmoney);
			// 预定按钮
			var button = $("<button>", {
				"data-id" : item.cid,
				"class" : "btn"
			}).text("预定");
			var car = $("<div>", {
				"class" : "car"
			}).append(image).append(info).append(span).append(button);
			$(".cars").append(car);
		});
	}
	/**
	 * 筛选分类标签栏
	 * 
	 * @param target
	 * @returns
	 */
	function select(target) {
		var text = target.text();
		var dataId = target.attr("id");
		var img = $("<span>", {
			"class" : "img"
		}).text("×");
		var div = $("<div>", {
			"class" : dataId
		}).text(text).append(img);
		if (target.parent().is(".brand")) {
			if (text != "全部") {
				if (target.is(".select")) {
					if (!target.siblings().is(".select")) {
						target.removeClass("select");
						target.siblings(".all").addClass("select");
						$(".brandSelected").html("");
						isNull();
					} else {
						target.removeClass("select");
						$(".brandSelected ." + dataId).remove();
					}
				} else {
					target.addClass("select");
					target.siblings(".all").removeClass("select");
					$(".tip").css("display", "none");
					$(".brandSelected").append(div);
				}
			} else {
				target.addClass("select");
				target.siblings().removeClass("select");
				$(".brandSelected").html("");
				isNull();
			}
		} else {
			target.addClass("select").siblings(".select").toggleClass("select");
			// 加入已选择分类中
			if (text != "全部") {
				if (target.parent().is(".style")) {
					$(".tip").css("display", "none");
					$(".styleSelected").html(div);
				} else {
					$(".tip").css("display", "none");
					$(".priceSelected").html(div);
				}
			} else {
				if (target.parent().is(".style")) {
					$(".styleSelected").html("");
				} else {
					$(".priceSelected").html("");
				}
				isNull();
			}
		}
	}
	/**
	 * 判断选中的标签栏是否为空
	 * 
	 * @returns
	 */
	function isNull() {
		if ($(".tag div").children().length == 0) {
			$(".tip").css("display", "block");
		}
	}
	/**
	 * 分页
	 */
	$(".M-box").pagination({
		pageCount:getCount(),
		/*totalData: $(".cars").attr("data-count"),
		showData:5,*/
        coping:true,
        homePage:'首页',
        endPage:'末页',
        prevContent:'上页',
        nextContent:'下页',
        callback:function(api){
        	/*console.log("当前页："+api.getCurrent());
    		console.log("总页："+api.getPageCount());
        	console.log(getCount());*/
        	api.setPageCount(getCount());//设置总页数
    		sendData($(".tag"),api.getCurrent());
        }
	});
	/**
	 * 获取记录总数
	 * @returns
	 */
	function getCount(){
		var style = $(".out").find(".style").find(".select").text();
		var brandAll = $(".out").find(".brand").find(".select");
		var maxIndex = brandAll.length - 1;
		var brand = "";
		// 拼接品牌的字符串
		brandAll.text(function(index, content) {
			brand += (index == maxIndex) ? "'" + content + "'" : "'" + content
					+ "',";
		});
		var price = $(".out").find(".price").find(".select")
				.text();
		// console.log(style+"和"+brand+"和"+price);
		var seq = $(".out").find(".ok").text();
		var count=0;
		$.ajax({
			type:"post",
			url:"show",
			data:{
				style : style,
				brand : brand,
				price : price,
				seq : seq},
			async: false,//同步
			success:function(datas) {
				count=datas;
				$(".cars").attr("data-count",datas);
			}
		});
		return Math.floor((Number(count)+5-1)/5);
	}
});